From 2b87350c7da5272ae6b20a398be11065f869792e Mon Sep 17 00:00:00 2001 From: tsteven4 Date: Sat, 9 May 2015 21:08:42 +0000 Subject: [PATCH] fix some undefined behaviors found by -fsanitize=undefined. --- gpsbabel/gpx.cc | 22 +++++++++++++++------- gpsbabel/holux.cc | 8 ++++++-- gpsbabel/saroute.cc | 7 +++++-- gpsbabel/unicsv.cc | 4 ++-- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/gpsbabel/gpx.cc b/gpsbabel/gpx.cc index 5360134a0..fd7be0c32 100644 --- a/gpsbabel/gpx.cc +++ b/gpsbabel/gpx.cc @@ -1850,25 +1850,33 @@ gpx_write(void) if (gpx_wversion_num > 10) { writer->writeStartElement("metadata"); } - gpx_write_gdata(&gpx_global->name, "name"); - gpx_write_gdata(&gpx_global->desc, "desc"); + if (gpx_global) { + gpx_write_gdata(&gpx_global->name, "name"); + gpx_write_gdata(&gpx_global->desc, "desc"); + } /* In GPX 1.1, author changed from a string to a PersonType. * since it's optional, we just drop it instead of rewriting it. */ if (gpx_wversion_num < 11) { - gpx_write_gdata(&gpx_global->author, "author"); + if (gpx_global) { + gpx_write_gdata(&gpx_global->author, "author"); + } } /* In GPX 1.1 email, url, urlname aren't allowed. */ if (gpx_wversion_num < 11) { - gpx_write_gdata(&gpx_global->email, "email"); - gpx_write_gdata(&gpx_global->url, "url"); - gpx_write_gdata(&gpx_global->urlname, "urlname"); + if (gpx_global) { + gpx_write_gdata(&gpx_global->email, "email"); + gpx_write_gdata(&gpx_global->url, "url"); + gpx_write_gdata(&gpx_global->urlname, "urlname"); + } } gpsbabel::DateTime now = current_time(); writer->writeTextElement("time", now.toPrettyString()); - gpx_write_gdata(&gpx_global->keywords, "keywords"); + if (gpx_global) { + gpx_write_gdata(&gpx_global->keywords, "keywords"); + } gpx_write_bounds(); diff --git a/gpsbabel/holux.cc b/gpsbabel/holux.cc index b17c5c0aa..8f2aa80a8 100644 --- a/gpsbabel/holux.cc +++ b/gpsbabel/holux.cc @@ -240,8 +240,12 @@ static void holux_disp(const Waypoint* wpt) } - le_write32(&pWptHxTmp->pt.iLatitude,(unsigned int) lat); - le_write32(&pWptHxTmp->pt.iLongitude,(unsigned int) lon); + // Note that conversions from double values to unsigned int + // yield undefined results for negative values. + // We intentionally convert to int, then do an implicit + // conversion to unsigned in the call. + le_write32(&pWptHxTmp->pt.iLatitude,(signed int) lat); + le_write32(&pWptHxTmp->pt.iLongitude,(signed int) lon); pWptHxTmp->checked = 01; pWptHxTmp->vocidx = (short)0xffff; le_write16(&((WPTHDR*)HxWFile)->num, ++sIndex); diff --git a/gpsbabel/saroute.cc b/gpsbabel/saroute.cc index 0350357d4..8c0ce54dc 100644 --- a/gpsbabel/saroute.cc +++ b/gpsbabel/saroute.cc @@ -127,6 +127,7 @@ my_read(void) int32_t lat; int32_t lon; } *latlon; + struct ll mylatlon; uint16_t coordcount; route_head* track_head = NULL; route_head* old_track_head = NULL; @@ -363,11 +364,13 @@ my_read(void) wpt_tmp = new Waypoint; + // copy to make sure we don't violate alignment restrictions. + memcpy(&mylatlon,latlon,sizeof(mylatlon)); lat = (0x80000000UL - - le_read32(&latlon->lat)) / + le_read32(&mylatlon.lat)) / (double)(0x800000); lon = (0x80000000UL - - le_read32(&latlon->lon)) / + le_read32(&mylatlon.lon)) / (double)(0x800000); wpt_tmp->latitude = lat; diff --git a/gpsbabel/unicsv.cc b/gpsbabel/unicsv.cc index b6723917e..72755c069 100644 --- a/gpsbabel/unicsv.cc +++ b/gpsbabel/unicsv.cc @@ -676,12 +676,12 @@ unicsv_parse_one_line(char* ibuf) switch (unicsv_fields_tab[column]) { case fld_latitude: - human_to_dec(CSTR(s), &wpt->latitude, &wpt->longitude, 1); + human_to_dec(CSTR(s), &wpt->latitude, NULL, 1); wpt->latitude = wpt->latitude * ns; break; case fld_longitude: - human_to_dec(CSTR(s), &wpt->latitude, &wpt->longitude, 2); + human_to_dec(CSTR(s), NULL, &wpt->longitude, 2); wpt->longitude = wpt->longitude * ew; break; -- 2.30.2